package org.intelli.dl.learn.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author: Michael
 * @date: 2025/4/22
 * @desc:
 */
@Component
public class RtGlobalFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();

        String uri = request.getURI().toString();
        long start = System.currentTimeMillis();

        System.out.println("请求" + uri + "开始,时间："+ start);

        Mono<Void> filter = chain.filter(exchange)
                .doFinally((result) -> {
                    long end = System.currentTimeMillis();
                    System.out.println("请求" + uri + "结束,时间："+ end + ",耗时：" + (end - start) + "ms");
                }); // 放行 10s

        return null;
    }

    @Override
    public int getOrder() {
        return 0;
    }

}
